package com.sx.micowell.module.switchlogin

import android.text.TextUtils
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.mvvm.bar.TitleBar
import com.example.mvvm.base.ContainerActivity
import com.example.mvvm.base.action.TitleBarAction
import com.example.mvvm.base.data.DataBindingConfig
import com.example.mvvm.rx.RxBus
import com.example.mvvm.until.ActivityManager
import com.example.mvvm.until.SPUtils
import com.example.mvvm.until.ToastUtils
import com.google.gson.Gson
import com.gyf.immersionbar.ImmersionBar

import com.miloyu.modelnet.response.LearnedCourseVO
import com.sx.micowell.BR
import com.sx.micowell.R
import com.sx.micowell.databinding.FragSwitchLoginBinding
import com.sx.micowell.framework.app.AppConstant
import com.sx.micowell.framework.app.MyFragment
import com.sx.micowell.framework.app.SPKey
import com.sx.micowell.framework.arouth.RouteManager
import com.sx.micowell.framework.arouth.RoutePath
import com.sx.micowell.framework.entity.AccountBean
import com.sx.micowell.framework.model.SettingModel
import com.sx.micowell.framework.tools.AppDialogUtils
import com.sx.micowell.framework.tools.EventInfo

/**
 * @Author: YY
 * @Time: 2021/9/10
 * @Describe 切换账号
 */
@Route(path = RoutePath.SWITCH_LOGIN)
class SwitchLoginFragment: MyFragment<FragSwitchLoginBinding, ContainerActivity>(), TitleBarAction {

    lateinit var viewModel: SettingModel

    private var switchListAdapter: SwitchListAdapter? = null

    private var switchList: List<AccountBean>? = null

    override fun initViewModel() {
        super.initViewModel()
        viewModel = getFragmentScopeViewModel(SettingModel::class.java)
    }

    override fun getDataBindingConfig(): DataBindingConfig {
        return DataBindingConfig(R.layout.frag_switch_login, BR.viewModel, viewModel)
    }

    override fun initView() {
        // 设置状态栏字体颜色
        ImmersionBar.with(this).statusBarDarkFont(true)
            .fitsSystemWindows(true).init()
        binding.title.setOnTitleBarListener(this)
    }

    override fun initData() {

        if (!TextUtils.isEmpty(SPUtils.getInstance().getString(SPKey.SP_ACCOUNT))) {
            val array: Array<AccountBean> = Gson().fromJson(SPUtils.getInstance().getString(SPKey.SP_ACCOUNT),
                Array<AccountBean>::class.java)
            switchList = array.toMutableList()
            setSwitchList()
        }
    }

    override fun initViewObservable() {
        super.initViewObservable()
        // 退出登录
        viewModel.loginOutEvent.observe(this, {
            AppDialogUtils.showBasicDialog(
                context, "提示", "确定要登录其他账号？",
                object : AppDialogUtils.IOnClickListener {
                    override fun leftListener() {}
                    override fun rightListener() {
                        viewModel.loginOut(true)
                    }
                })
        })

        // 退出成功
        viewModel.loginOutStateEvent.observe(this, {
            // 清除token、手机号
            SPUtils.getInstance().put(SPKey.SP_TOKEN, "")
            SPUtils.getInstance().put(SPKey.SP_PHONE, "")
            RxBus.getDefault().post(EventInfo(AppConstant.EVENT_LOGIN_OUT))
            mActivity.finish()
        })

    }

    override fun getTitleBar(): TitleBar {
        return binding.title
    }

    override fun onLeftClick(v: View?) {
        myActivity.finish()
    }

    private fun setSwitchList() {
        //取消recyclerview单独的滑动效果
        binding.recyclerSwitch.isNestedScrollingEnabled = false
        //设置布局管理器
        binding.recyclerSwitch.layoutManager = LinearLayoutManager(context)

        if (switchList != null && switchList?.size!! > 0 ) {

            if (switchListAdapter == null) {
                switchListAdapter = SwitchListAdapter()
                switchListAdapter!!.data = switchList as MutableList<AccountBean>
                binding.recyclerSwitch.adapter = switchListAdapter

                switchListAdapter!!.setOnItemClickListener { adapter, view, position ->
                   if (position == 0) {
                       return@setOnItemClickListener
                   }
                    val accountBean: AccountBean = adapter.getItem(position) as AccountBean
                    SPUtils.getInstance().put(SPKey.SP_TOKEN, accountBean.token)
                    SPUtils.getInstance().put(SPKey.SP_PHONE, accountBean.phone)
                    viewModel.saveAccount(accountBean.phone,accountBean.pwd,accountBean.token)
                    ActivityManager.getInstance().exit()
                    RouteManager.getInstance().intentToMain()
                }

            } else {
                switchListAdapter!!.data = switchList as MutableList<AccountBean>
                binding.recyclerSwitch.adapter = switchListAdapter
            }
        }

    }
}